package com.turbo.cloud.contract.powerjob.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.powerjob.common.enums.Protocol;
import tech.powerjob.worker.PowerJobSpringWorker;
import tech.powerjob.worker.common.PowerJobWorkerConfig;
import tech.powerjob.worker.common.constants.StoreStrategy;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Configuration
public class PowerJobWorkerConfiguration {
    /**
     * 磁盘
     */
    private static final String DISK = "DISK";
    /**
     * 内存
     */
    private static final String MEMORY = "MEMORY";

    /**
     * 协议模式
     */
    private static final String AKKA = "AKKA";

    /**
     * HTTP
     */
    private static final String HTTP = "HTTP";

    /**
     * 应用名称
     */
    @Value("${powerjob.worker.app-name}")
    private String appName;
    /**
     * 服务地址
     */
    @Value("${powerjob.worker.server-address}")
    private String serverAddress;
    /**
     * 端口
     */
    @Value("${powerjob.worker.port}")
    private Integer port;
    /**
     * 存储策略
     */
    @Value("${powerjob.worker.store-strategy}")
    private String storeStrategy;
    /**
     * Http协议
     */
    @Value("${powerjob.worker.protocol}")
    private String protocol;
    /**
     * Http协议
     */
    @Value("${powerjob.worker.max-result-length}")
    private Integer maxResultLength;
    /**
     * Http协议
     */
    @Value("${powerjob.worker.max-appended-wf-context-length}")
    private Integer maxAppendedWfContextLength;
    /**
     * Http协议
     */
    @Value("${powerjob.worker.enable-test-mode}")
    private Boolean enableTestMode;

    @Bean
    public PowerJobSpringWorker initPowerJobWorker() throws Exception {
        // 1. 创建配置文件
        PowerJobWorkerConfig config = new PowerJobWorkerConfig();
        config.setAppName(appName);

        if (StrUtil.isNotBlank(serverAddress)) {
            List<String> serverAddressList = Arrays.asList(serverAddress.split(","));
            config.setServerAddress(serverAddressList);
        }

        config.setPort(port);

        // 如果没有大型 Map/MapReduce 的需求，建议使用内存来加速计算
        if (DISK.equalsIgnoreCase(storeStrategy)) {
            config.setStoreStrategy(StoreStrategy.DISK);
        } else {
            config.setStoreStrategy(StoreStrategy.MEMORY);
        }

        if (AKKA.equalsIgnoreCase(protocol)) {
            config.setProtocol(Protocol.AKKA);
        } else {
            config.setProtocol(Protocol.HTTP);
        }

        if (Objects.nonNull(maxResultLength)) {
            config.setMaxResultLength(maxResultLength);
        }

        if (Objects.nonNull(maxAppendedWfContextLength)) {
            config.setMaxAppendedWfContextLength(maxAppendedWfContextLength);
        }

        if (Objects.nonNull(enableTestMode)) {
            config.setEnableTestMode(enableTestMode);
        }

        // 2. 创建 Worker 对象，设置配置文件（注意 Spring 用户需要使用 PowerJobSpringWorker，而不是 PowerJobWorker）
        PowerJobSpringWorker worker = new PowerJobSpringWorker(config);
        return worker;
    }
}
